<template>
  <div class="returns-list">
    <el-row :gutter="10" justify="end">
      <el-col :span="3.5"><el-input placeholder="输入仓库名字查询" v-model.trim="store_name" /></el-col>
      <el-col :span="3.5"><el-input placeholder="输入供应商名字查询" v-model.trim="supplier_name" /></el-col>
      <el-col :span="3.5">
        <el-form-item>
          <el-select
            v-model="state"
            placeholder="请选择"
            style="width: 240px"
            filterable
            no-match-text="没有这个选项"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="1"><el-button type="primary" @click="handleSearch">查询</el-button></el-col>
      <el-col :span="1"><el-button @click="handleEdit">新建</el-button></el-col>
    </el-row>
    <el-divider />
    <el-table
    style="width: 100%"
    border
    :header-cell-style="{background:'rgb(250,250,250)'}"
    :data="returnsList">
      <el-table-column prop="id" label="编码" width="320" />
      <el-table-column prop="state" label="状态" width="160" />
      <el-table-column prop="supplier_name" label="供应商名字" width="220" />
      <el-table-column prop="supplier_contact" label="联系人" width="170" />
      <el-table-column prop="store_name" label="仓库名字" width="270" />
      <el-table-column prop="remark" label="备注" width="320" />
      <el-table-column label="操作" width="220">
        <a href="./detail">详情</a>&nbsp;
        <a href="#">审批</a>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>

    <el-pagination
    background
    layout="prev, pager, next"
    :total="total"
    :page-size="sizes"
    @current-change="handlePage"
    />
  </div>
</template>

<script>
import { addReturnsApi,approveReturnsApi,getReturnsApi } from '@/apis/Purchase/returns'

export default {
  data() {
    return {
      store_name: '',
      supplier_name: '',
      state: '',
      sizes: 10,
      total: 0,
      pages: 1,
      returnsList: [],
      options: [
        {
          value: '',
          label: '全部',
        },
        {
          value: 'New',
          label: '新建',
        },
        {
          value: 'Finance',
          label: '下发订单',
        },
        {
          value: 'Issue',
          label: '供应商确认',
        },
        {
          value: 'Archive',
          label: '归档',
        },
        {
          value: 'Cancel',
          label: '取消',
        },
      ],
    }
  },
  created() {
    this.handleGet()
  },
  methods: {
    handleEdit() {
      this.$router.push('./edit')
    },
    
    async handleGet(store_name, supplier_name, state, size = this.sizes, page = this.pages) {
      try {
        const res = await getReturnsApi({store_name, supplier_name, state, size, page})
        if(res.code === 200) {
          this.returnsList = res.data.rows
          this.total = res.data.count
          this.size = size
        }

      } catch (error) {
        console.log(error)
      }
    },
    handleSearch() {
      this.returnsList.forEach(item => {
        if(item.store_name.includes(this.store_name)) {
          this.handleGet(this.store_name, this.supplier_name, this.state)
        }
        else if(item.supplier_name.includes(this.supplier_name)) {
          this.handleGet(this.store_name, this.supplier_name, this.state)
        }
        else if(item.state === this.state) {
          this.handleGet(this.store_name, this.supplier_name, this.state)
        }
      })
    },
    handlePage(e) {
      this.pages = e;
      this.handleGet()
    }
  }
}


</script>

<style lang="less">
.el-pagination {
  width: 300px;
  position: absolute;
  left: 45%;
  margin-top: 30px;
}

.el-table--border,
.el-table--group {
  border: 1px solid rgb(236, 236, 236);
}

a {
  text-decoration: none;
}
</style>
